#include "adminselectemployeeview.h"
#include <QHeaderView>
#include <QSqlError>
#include <QDebug>

AdminSelectEmployeeView::AdminSelectEmployeeView(QWidget *parent) : QWidget(parent)
{
    // 初始化管理员对象
    adminManager = new AdminManager(this);

    setupUI();
    setupConnections();
    initTable();

    // 连接AdminManager的信号
    connect(adminManager, &AdminManager::operationResult, this, [this](bool success, const QString &message) {
        if (!success) {
            QMessageBox::critical(this, "错误", message);
        }
    });
}

AdminSelectEmployeeView::~AdminSelectEmployeeView()
{
    delete adminManager;
}

void AdminSelectEmployeeView::setupUI()
{
    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 创建查询条件区域
    QWidget *searchWidget = new QWidget(this);
    QVBoxLayout *searchLayout = new QVBoxLayout(searchWidget);

    // 查询条件标题
    QLabel *searchTitle = new QLabel("员工信息查询");
    QFont titleFont("Microsoft YaHei", 12, QFont::Bold);
    searchTitle->setFont(titleFont);
    searchTitle->setAlignment(Qt::AlignCenter);
    searchLayout->addWidget(searchTitle);

    // 查询条件表单
    QGridLayout *formLayout = new QGridLayout();

    // 员工ID
    QLabel *idLabel = new QLabel("员工ID:");
    idEdit = new QLineEdit();
    formLayout->addWidget(idLabel, 0, 0);
    formLayout->addWidget(idEdit, 0, 1);

    // 员工姓名
    QLabel *nameLabel = new QLabel("员工姓名:");
    nameEdit = new QLineEdit();
    formLayout->addWidget(nameLabel, 1, 0);
    formLayout->addWidget(nameEdit, 1, 1);

    // 部门
    QLabel *departmentLabel = new QLabel("部门:");
    departmentEdit = new QLineEdit();
    formLayout->addWidget(departmentLabel, 2, 0);
    formLayout->addWidget(departmentEdit, 2, 1);

    searchLayout->addLayout(formLayout);

    // 按钮区域
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    searchButton = new QPushButton("查询");
    resetButton = new QPushButton("重置");
    buttonLayout->addWidget(searchButton);
    buttonLayout->addWidget(resetButton);
    searchLayout->addLayout(buttonLayout);

    // 创建结果表格
    resultTable = new QTableWidget();
    resultTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    resultTable->setSelectionMode(QAbstractItemView::SingleSelection);
    resultTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    resultTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    // 添加到主布局
    mainLayout->addWidget(searchWidget);
    mainLayout->addWidget(resultTable);

    setLayout(mainLayout);
    setMinimumSize(800, 600);
}

void AdminSelectEmployeeView::setupConnections()
{
    // 连接按钮信号和槽
    connect(searchButton, &QPushButton::clicked, this, &AdminSelectEmployeeView::onSearchButtonClicked);
    connect(resetButton, &QPushButton::clicked, this, &AdminSelectEmployeeView::onResetButtonClicked);
}

void AdminSelectEmployeeView::initTable()
{
    // 设置表格列
    QStringList headers;
    headers << "员工ID" << "姓名" << "性别" << "职位" << "部门";
    resultTable->setColumnCount(headers.size());
    resultTable->setHorizontalHeaderLabels(headers);

    // 初始化时自动查询所有员工信息（不显示弹框）
    searchEmployees(false);
}

void AdminSelectEmployeeView::onSearchButtonClicked()
{
    // 执行查询（显示弹框）
    searchEmployees(true);
}

void AdminSelectEmployeeView::onResetButtonClicked()
{
    // 清空输入框
    idEdit->clear();
    nameEdit->clear();
    departmentEdit->clear();
    // 重置后自动查询所有员工信息（不显示弹框）
    searchEmployees(false);
}

void AdminSelectEmployeeView::searchEmployees(bool showMessageBox)
{
    // 获取查询条件
    QString id = idEdit->text().trimmed();
    QString name = nameEdit->text().trimmed();
    QString department = departmentEdit->text().trimmed();

    // 调用AdminManager的查找方法
    QList<QVariantList> results = adminManager->searchEmployees(id, name, department);

    // 清空表格
    resultTable->setRowCount(0);

    // 填充表格
    for (int i = 0; i < results.size(); ++i)
    {
        const QVariantList &rowData = results.at(i);
        resultTable->insertRow(i);
        for (int j = 0; j < rowData.size(); ++j)
        {
            resultTable->setItem(i, j, new QTableWidgetItem(rowData.at(j).toString()));
        }
    }

    // 仅在需要时显示查询结果数量
    if (showMessageBox) {
        QMessageBox::information(this, "查询完成", QString("共找到 %1 条记录").arg(results.size()));
    }
}
